﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       Shpm.System.Web.Control.ComValidator
 * 类型：       服务端输入校验类
 * 功能说明：   对输入内容进行合法性校验
 * 作者：       王伟栋
 * 创建日期：   2007/07/09
 * 
 * 修改日期     修改者      标记           修改内容     
 * 2007/09/10   章 昕      UPD001         增加校验方法 Validate，
 *                                        该方法可以对校验类实例 直接进行校验          
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Drawing.Design;
using System.Security.Permissions;
using Shpm.System.Web.Control;
using System.Resources;


namespace Shpm.System.Web.Control
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [DefaultProperty("CheckItems")]
    [ParseChildren(true, "CheckItems")]
    [ToolboxData("<{0}:ComValidator runat=\"server\"> </{0}:ComValidator>")]
    public class ComValidator : WebControl
    {
        /*
         ************************************************
         * 类变量定义
         ************************************************
        */
        /** 包含校验类的集合 */
        private ArrayList checkItems = new ArrayList();
        /** 出错代码 */
        private string errorCode = string.Empty;
        /** 出错消息 */
        private string errorMessage = string.Empty;

        /*
         ************************************************
         * 属性定义
         ************************************************
        */


        /** 包含校验类的集合 */
        [Category("Behavior")]
        [Description("")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(ComValidatorEditor), typeof(UITypeEditor))]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public ArrayList CheckItems
        {
            get
            {
                return checkItems;
            }
        }

        /** 出错代码 */
        public string ErrorCode
        {
            get
            {
                return errorCode;
            }
        }
        
        /** 出错消息 */
        public string ErrorMessage
        {
            get
            {
                return errorMessage;
            }
            set 
            {
                errorMessage = value;
            }
        }

        public ComValidator()
            : base(HtmlTextWriterTag.Font) 
        {

        }

        /*向HtmlTextWriter流中添加属性*/
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddAttribute("color", this.ForeColor.Name);
            writer.AddAttribute("class", this.CssClass);
            base.AddAttributesToRender(writer);

        }

        /*HtmlTextWriter流输出*/
        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (this.DesignMode)
            {
                writer.Write(this.ID);
                writer.AddAttribute("color", this.ForeColor.Name);
            }
            else 
            { 
                writer.Write(this.errorMessage); 
            }
           
            base.RenderContents(writer);
        }

        /*
         *名称： DoValidator
         *功能： 对集合中包含的校验类进行校验，并返回校验结果。
         *参数： 无
         *返回： 校验成功,True 
         *       校验失败,False
         *说明： 
        */
        public bool DoValidator()
        {
            
            IEnumerator enm = this.checkItems.GetEnumerator();
            while (enm.MoveNext())
            {
                ComAbsCheckerAttribute attr = (ComAbsCheckerAttribute)enm.Current;
                ComAbsChecker checker = null;

                if (attr is CheckTypeAttribute)
                {
                    checker = new CheckType(CheckUtils.GetValue(this.FindControl((attr as CheckTypeAttribute).ControlToValidator) as WebControl), (attr as CheckTypeAttribute).Pattern);
                }
                else if (attr is CheckFileTypeStrAttribute)
                {
                    checker = new CheckFileTypeStr(CheckUtils.GetValue(this.FindControl((attr as CheckFileTypeStrAttribute).ControlToValidator) as WebControl), (attr as CheckFileTypeStrAttribute).Types);
                }
                else if (attr is CheckRequiredAttribute)
                {
                    checker = new CheckRequired(CheckUtils.GetValue(this.FindControl((attr as CheckRequiredAttribute).ControlToValidator) as WebControl));
                 }
                else if (attr is CheckLongAttribute)
                {
                    checker = new CheckLong(CheckUtils.GetValue(this.FindControl((attr as CheckLongAttribute).ControlToValidator) as WebControl));
                }
                else if (attr is CheckLongRangeAttribute)
                {
                    checker = new CheckLongRange(CheckUtils.GetValue(this.FindControl((attr as CheckLongRangeAttribute).ControlToValidator) as WebControl)
                        , (attr as CheckLongRangeAttribute).MinNumber, (attr as CheckLongRangeAttribute).MaxNumber );
                }
                else if (attr is CheckDateAttribute)
                {
                    checker = new CheckDate(CheckUtils.GetValue(this.FindControl((attr as CheckDateAttribute).ControlToValidator) as WebControl), (attr as CheckDateAttribute).DataFormat);
                }
                else if (attr is CheckDateRangeAttribute)
                {
                    checker = new CheckDateRange(CheckUtils.GetValue(this.FindControl((attr as CheckDateRangeAttribute).ControlToValidator1) as WebControl),
                        CheckUtils.GetValue(this.FindControl((attr as CheckDateRangeAttribute).ControlToValidator2) as WebControl),(attr as CheckDateRangeAttribute).DataFormat);
                }
                else if (attr is CheckFullAttribute)
                {
                    checker = new CheckFull(CheckUtils.GetValue(this.FindControl((attr as CheckFullAttribute).ControlToValidator) as WebControl));
                }
                else if (attr is CheckHalfAttribute)
                {
                    checker = new CheckHalf(CheckUtils.GetValue(this.FindControl((attr as CheckHalfAttribute).ControlToValidator) as WebControl));
                }
                else if (attr is CheckDoubleAttribute)
                {
                    checker = new CheckDouble(CheckUtils.GetValue(this.FindControl((attr as CheckDoubleAttribute).ControlToValidator) as WebControl)
                         , (attr as CheckDoubleAttribute).IntLen, (attr as CheckDoubleAttribute).DecLen);
                }
                else if (attr is CheckDoubleLessAttribute)
                {
                    checker = new CheckDoubleLess(CheckUtils.GetValue(this.FindControl((attr as CheckDoubleLessAttribute).ControlToValidator) as WebControl)
                         , (attr as CheckDoubleLessAttribute).Less);
                }
                else if (attr is CheckDoubleMoreAttribute)
                {
                    checker = new CheckDoubleMore(CheckUtils.GetValue(this.FindControl((attr as CheckDoubleMoreAttribute).ControlToValidator) as WebControl)
                         , (attr as CheckDoubleMoreAttribute).More);
                }
                else if (attr is CheckEmailAttribute)
                {
                    checker = new CheckEmail(CheckUtils.GetValue(this.FindControl((attr as CheckEmailAttribute).ControlToValidator) as WebControl));
                }
                else if (attr is CheckPhoneAttribute)
                {
                    checker = new CheckPhone(CheckUtils.GetValue(this.FindControl((attr as CheckPhoneAttribute).ControlToValidator) as WebControl));
                }
                else if (attr is CheckPostAttribute)
                {
                    checker = new CheckPost(CheckUtils.GetValue(this.FindControl((attr as CheckPostAttribute).ControlToValidator) as WebControl));
                }
                else if (attr is CheckMaxCharLenAttribute)
                {
                    checker = new CheckMaxCharLen(CheckUtils.GetValue(this.FindControl((attr as CheckMaxCharLenAttribute).ControlToValidator) as WebControl)
                        , (attr as CheckMaxCharLenAttribute).CharLen);
                }
                else if (attr is CheckMaxByteLenAttribute)
                {
                    checker = new CheckMaxByteLen(CheckUtils.GetValue(this.FindControl((attr as CheckMaxByteLenAttribute).ControlToValidator) as WebControl)
                        , (attr as CheckMaxByteLenAttribute).ByteLen, (attr as CheckMaxByteLenAttribute).CharCode);
                }
                else if (attr is CheckMinByteLenAttribute)
                {
                    checker = new CheckMinByteLen(CheckUtils.GetValue(this.FindControl((attr as CheckMinByteLenAttribute).ControlToValidator) as WebControl)
                        , (attr as CheckMinByteLenAttribute).ByteLen);
                }
                else if (attr is CheckMinCharLenAttribute)
                {
                    checker = new CheckMinCharLen(CheckUtils.GetValue(this.FindControl((attr as CheckMinCharLenAttribute).ControlToValidator) as WebControl)
                        , (attr as CheckMinCharLenAttribute).CharLen);
                }

                else if (attr is CheckFileExistAttribute)
                {
                    checker = new CheckFileExist(CheckUtils.GetValue(this.FindControl((attr as CheckFileExistAttribute).ControlToValidator) as WebControl)
                        , this.FindControl((attr as CheckFileExistAttribute).ControlToValidator) as WebControl);
                }
                else if (attr is CheckFaxAttribute)
                {
                    checker = new CheckFax(CheckUtils.GetValue(this.FindControl((attr as CheckFaxAttribute).ControlToValidator) as WebControl));
                }
                else if (attr is CheckFaxAttribute)
                {
                    checker = new CheckFax(CheckUtils.GetValue(this.FindControl((attr as CheckFaxAttribute).ControlToValidator) as WebControl));
                }

                else if (attr is CheckLongLessAttribute)
                {
                    checker = new CheckLongLess(CheckUtils.GetValue(this.FindControl((attr as CheckLongLessAttribute).ControlToValidator) as WebControl)
                         , (attr as CheckLongLessAttribute).Less);
                }
                else if (attr is CheckLongMoreAttribute)
                {
                    checker = new CheckLongMore(CheckUtils.GetValue(this.FindControl((attr as CheckLongMoreAttribute).ControlToValidator) as WebControl)
                         , (attr as CheckLongMoreAttribute).More);
                }
                else if (attr is CheckLongMoreAttribute)
                {
                    checker = new CheckLongMore(CheckUtils.GetValue(this.FindControl((attr as CheckLongMoreAttribute).ControlToValidator) as WebControl)
                         , (attr as CheckLongMoreAttribute).More);
                }
                else if (attr is CheckDataExistsAttribute)
                {
                    CheckDataExistsAttribute dataExistsAttr = attr as CheckDataExistsAttribute;
                    checker = new CheckDataExists(dataExistsAttr.Dao, dataExistsAttr.SqlId, dataExistsAttr.ParamBean);
                }
                else if (attr is CheckSpecLenAttribute)
                {
                    checker = new CheckSpecLen(CheckUtils.GetValue(this.FindControl((attr as CheckSpecLenAttribute).ControlToValidator) as WebControl)
                         , (attr as CheckSpecLenAttribute).SpecLen);
                }
                
                if (checker!=null && checker.doCheck() == false)
                {
                    this.errorCode = attr.Code;
                    this.errorMessage = getMessage(attr.Code,attr.Parameters);
                    return false;
                }
            }

            return true;
        }

        // UPD001 START
        // using check class directly
        public bool Validate()
        {
            try
            {
                IEnumerator enm = this.checkItems.GetEnumerator();
                while (enm.MoveNext())
                {
                    IFChecker checker = (IFChecker)enm.Current;
                    if (checker.doCheck() == false)
                    {
                        this.errorMessage = checker.getMessage();
                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                this.errorMessage = "SYSTEM ERROR!";
                //log.fatal(e.StackTrace);
                return false;
            }
            return true;
        }
        // UPD001 END

        /*错误消息获取 */
        private string getMessage(string code, IList Parameters)
        {
            String msg = "";
            string str = HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, code) as string;
            if (Parameters.Count == 0)
            {
                msg = str;
            }
            else if( Parameters.Count == 1)
            {
                ComAbsParamsAttribute at = Parameters[0] as ComAbsParamsAttribute;
                msg = string.Format(str, (Parameters[0] as ComAbsParamsAttribute).Parameter);
            }
            else if (Parameters.Count == 2)
            {
                msg = string.Format(str, (Parameters[0] as ComAbsParamsAttribute).Parameter, (Parameters[1] as ComAbsParamsAttribute).Parameter);
            }
            else if (Parameters.Count == 3)
            {
                msg = string.Format(str, (Parameters[0] as ComAbsParamsAttribute).Parameter, (Parameters[1] as ComAbsParamsAttribute).Parameter, (Parameters[2] as ComAbsParamsAttribute).Parameter);
            }

            return msg;
        }
    }
}
